package com.example.managemyself.ui.admin;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.managemyself.MainActivity;
import com.example.managemyself.R;
import com.example.managemyself.ui.admin.dashboard.AdminDashboardFragment;
import com.example.managemyself.ui.admin.users.UserManagementFragment;
import com.example.managemyself.ui.admin.data.DataManagementFragment;
import com.example.managemyself.ui.admin.settings.AdminSettingsFragment;
import com.example.managemyself.util.UserManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

/**
 * 管理员主界面Activity
 */
public class AdminMainActivity extends AppCompatActivity {
    
    private BottomNavigationView bottomNavigation;
    private UserManager userManager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_main);
        
        // 验证管理员权限
        userManager = UserManager.getInstance();
        if (!userManager.isCurrentUserAdmin()) {
            Toast.makeText(this, "无管理员权限，返回普通用户界面", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return;
        }
        
        initViews();
        setupBottomNavigation();
        
        // 默认显示仪表板
        if (savedInstanceState == null) {
            loadFragment(new AdminDashboardFragment());
        }
    }
    
    private void initViews() {
        bottomNavigation = findViewById(R.id.admin_bottom_navigation);
        
        // 设置标题
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("管理员控制台");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
    
    private void setupBottomNavigation() {
        bottomNavigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                
                int itemId = item.getItemId();
                if (itemId == R.id.admin_nav_dashboard) {
                    selectedFragment = new AdminDashboardFragment();
                } else if (itemId == R.id.admin_nav_users) {
                    selectedFragment = new UserManagementFragment();
                } else if (itemId == R.id.admin_nav_data) {
                    selectedFragment = new DataManagementFragment();
                } else if (itemId == R.id.admin_nav_settings) {
                    selectedFragment = new AdminSettingsFragment();
                }
                
                if (selectedFragment != null) {
                    loadFragment(selectedFragment);
                    return true;
                }
                return false;
            }
        });
    }
    
    private void loadFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.admin_fragment_container, fragment)
                .commit();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.admin_main_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        
        if (itemId == android.R.id.home) {
            // 返回普通用户界面
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return true;
        } else if (itemId == R.id.action_switch_to_user) {
            // 切换到普通用户模式
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return true;
        } else if (itemId == R.id.action_logout) {
            // 退出登录
            userManager.logout(this, new UserManager.SimpleCallback() {
                @Override
                public void onSuccess() {
                    // 跳转到登录界面
                    Intent intent = new Intent(AdminMainActivity.this, com.example.managemyself.ui.auth.LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
                }
                
                @Override
                public void onError(String error) {
                    Toast.makeText(AdminMainActivity.this, "登出失败: " + error, Toast.LENGTH_SHORT).show();
                }
            });
            return true;
        }
        
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 再次验证管理员权限
        if (!userManager.isCurrentUserAdmin()) {
            Toast.makeText(this, "管理员权限已失效", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }
} 